#!/usr/bin/env python

import sqlite3

con = sqlite3.connect("./photos.db")
cur = con.cursor()

match_query = """
select id, uri 
from photos
where uri like "%%%(pattern)s%%"
;
"""
update = """
update photos
set uri = "%s"
where id = %s
;
"""

#pattern = "/home/akuntz/Bilder/"
pattern = "/media/Elements/"
subst   = "/media/Archive/photo/"

cur.execute(match_query%{'pattern':pattern})
#cur.fetchall()
for item in cur:
    print item
    cmd = update%(item[1].replace(pattern, subst), item[0])
    print cmd

    c = con.cursor()
    c.execute(cmd)

con.commit()
